//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Platforms/Base/IApplication.h"

#if defined( PLATFORM_ANDROID )

namespace Engine
{
namespace Platforms
{

	//
	// Android Window Surface
	//

	struct AndroidWindowSurface
	{
	// variables
	private:
		android::ANativeWindow *	_window;


	// methods
	public:
		AndroidWindowSurface () : _window(null)
		{}

		AndroidWindowSurface (android::JNIEnv *env, android::jobject surface) : _window(null)
		{
			using namespace android;

			if ( surface != null )
				_window = ANativeWindow_fromSurface( env, surface );

			LOG( (string("Create window: ") << ReferenceCast<usize>(_window)).cstr(), ELog::INFO );
		}

		AndroidWindowSurface (const AndroidWindowSurface &other) : _window(null)
		{
			Set( other._window );
			
			LOG( (string("Create window: ") << ReferenceCast<usize>(_window)).cstr(), ELog::INFO );
		}

		RVALUE_REFERENCE_ONLY(
		AndroidWindowSurface (AndroidWindowSurface &&other) : _window(null)
		{
			_window = other._window;
			other._window = null;
		})

		~AndroidWindowSurface ()
		{
			Destroy();
		}

		void Set (android::ANativeWindow *window)
		{
			using namespace android;

			Destroy();

			if ( window != null )
			{
				ANativeWindow_acquire( window );
				_window = window;
			}
		}

		android::ANativeWindow * Get () const
		{
			return _window;
		}

		bool IsValid () const
		{
			return _window != null;
		}

		AndroidWindowSurface & operator = (const AndroidWindowSurface &right)
		{
			Set( right._window );
			return *this;
		}

		bool operator == (const AndroidWindowSurface &right) const
		{
			return _window == right._window;
		}

		void Destroy ()
		{
			using namespace android;

			if ( _window != null )
			{
				LOG( (string("Release window: ") << ReferenceCast<usize>(_window)).cstr(), ELog::INFO );
				ANativeWindow_release( _window );
			}

			_window = null;
		}
	};

}	// Platforms
}	// Engine

#endif	// PLATFORM_ANDROID